package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsWnsChargeClassificationRelation;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsWnsChargeClassificationRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsChargeCategory;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsChargeCategoryDetail;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsChargeCategoryReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsIdListReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsChargeCategoryDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsChargeCategoryService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 收费类别表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-01-15
 */
@RestController
@SecretBody
@RequestMapping("/welcomeNewStudent/chargeCategory")
public class SmsWnsChargeCategoryController {
    @Resource
    private SmsWnsChargeCategoryService smsWnsChargeCategoryService;
    @Resource
    private SmsWnsChargeCategoryDetailMapper smsWnsChargeCategoryDetailMapper;
    @Resource
    private SmsWnsChargeClassificationRelationMapper smsWnsChargeClassificationRelationMapper;

    @ApiModelProperty("添加收费类别")
    @PostMapping("/add")
    public ResponseResult addCategory(@RequestBody SmsWnsChargeCategoryReq categoryReq) {
        return smsWnsChargeCategoryService.addCategory(categoryReq);
    }

    @ApiModelProperty("删除收费类别")
    @PostMapping("/del")
    public ResponseResult delCategory(@RequestBody SmsWnsIdListReq idListReq) {
        if (idListReq.getIdList().isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        Integer count = smsWnsChargeClassificationRelationMapper.selectCount(new LambdaQueryWrapper<SmsWnsChargeClassificationRelation>()
                .in(SmsWnsChargeClassificationRelation::getChargeDetailId, idListReq.getIdList()));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.WNS_CHARGE_CATEGORY_PLEASE_DELETE_CLASSIFICATION_BINDING);
        }

        // 删除收费类别详情
        for (String categoryId : idListReq.getIdList()) {
            SmsWnsChargeCategory chargeCategory = smsWnsChargeCategoryService.getById(categoryId);
            if (chargeCategory == null || !chargeCategory.getIsModified()) {
                return CommonResult.failed(CommonCodeEnum.WNS_CHARGE_CATEGORY_CAN_NOT_UPDATE_OR_DELETE);
            }

            List<SmsWnsChargeCategoryDetail> detailList = smsWnsChargeCategoryDetailMapper.selectList(new LambdaQueryWrapper<SmsWnsChargeCategoryDetail>()
                    .eq(SmsWnsChargeCategoryDetail::getCategoryId, categoryId));
            if (!detailList.isEmpty()) {
                List<String> detailIdList = detailList.stream().map(SmsWnsChargeCategoryDetail::getId).collect(Collectors.toList());
                smsWnsChargeCategoryDetailMapper.deleteBatchIds(detailIdList);
            }
        }

        smsWnsChargeCategoryService.removeByIds(idListReq.getIdList());
        return CommonResult.success();
    }

    @ApiModelProperty("修改收费类别")
    @PostMapping("/update")
    public ResponseResult updateCategory(@RequestBody SmsWnsChargeCategoryReq categoryReq) {
        return smsWnsChargeCategoryService.updateCategory(categoryReq);
    }

    @ApiModelProperty("收费类别列表")
    @GetMapping("/list")
    public ResponseResult listCategory(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(value = "searchStr", required = false) String searchStr) {
        return smsWnsChargeCategoryService.listCategory(pageNum, pageSize, searchStr);
    }

    @ApiModelProperty("收费类别详情")
    @GetMapping("/detail")
    public ResponseResult categoryDetail(@RequestParam("categoryId") String categoryId) {
        return smsWnsChargeCategoryService.categoryDetail(categoryId);
    }

}

